var app = getApp();

Page({
  data: {
    device: [
      { iconurl: '/images/icon/icon/wx_app_clear.png', title: '阅读设置', tap: 'scanSetting' },
      { iconurl: '/images/icon/icon/wx_app_compass.png', title: '我的收藏', tap: 'showFavorite' },
      { iconurl: '/images/icon/icon/wx_app_cellphone.png', title: '授权管理', tap: 'showPower' },
      { iconurl: '/images/icon/icon/wx_app_location.png', title: '我的位置', tap: 'showMap' }
    ],
    compassVal: 0,
    compassHidden: true,
    shakeInfo: {
      gravityModalHidden: true,
      num: 0,
      enabled: false
    },
    shakeData: {
      x: 0,
      y: 0,
      z: 0
    },
  },

  onLoad: function () {
    var that = this
    //调用应用实例的方法获取全局数据
    app.getUserInfo(function (userInfo) {
      //更新数据
      that.setData({
        userInfo: userInfo
      })
    })
    var that = this;

    wx.getStorage({
      key: 'collectedId',
      success: function (res) {
        that.setData({
          collectedId: res.data
        })

      }
    })
  },
  showFavorite:function(){
   
    wx.navigateTo({
      url: 'favorite/favorite?collectedId=' + this.data.collectedId
    });
  },
  scanSetting:function(){
       wx.navigateTo({
         url: 'scanSetting/scanSetting'
       });
  },

  //显示模态窗口
  showModal: function (title, content, callback) {
    wx.showModal({
      title: title,
      content: content,
      confirmColor: '#1F4BA5',
      cancelColor: '#7F8389',
      success: function (res) {
        if (res.confirm) {
          callback && callback();
        }
      }
    })
  },

  //显示授权信息
  showPower: function () {
    wx.openSetting({
      success: (res) => {
      }
    })
  },

  //获取当前位置经纬度与当前速度
  getLonLat: function (callback) {
    var that = this;
    wx.getLocation({
      type: 'gcj02',
      success: function (res) {
        console.log(res)
        callback(res.longitude, res.latitude, res.speed);
      }
    });
  },

  //显示当前位置坐标与当前速度
  showLonLat: function () {
    var that = this;
    this.getLonLat(function (lon, lat, speed) {
      var lonStr = lon >= 0 ? '东经' : '西经',
        latStr = lat >= 0 ? '北纬' : '南纬';
      lon = lon.toFixed(2);
      lat = lat.toFixed(2);
      lonStr += lon;
      latStr += lat;
      speed = (speed || 0).toFixed(2);
      that.showModal('当前位置和速度', '当前位置：' + lonStr + ',' + latStr + '。速度:' + speed + 'm/s');
    });
  },

  //在地图上显示当前位置
  showMap: function () {
    this.getLonLat(function (lon, lat) {
      wx.openLocation({
        latitude: lat,
        longitude: lon,
        scale: 15,
        name: "兰州理工大学西校区",
        address: "南G718",
        fail: function () {
          wx.showToast({
            title: "地图打开失败",
            duration: 1000,
            icon: "cancel"
          });
        }
      });
    });
  }

})